package com.ljs.controller;

import com.ljs.entity.Customer;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class CustomerHanddler {
    @Resource
    RestTemplate restTemplate;
    @RequestMapping("/find")
    public List<Customer> find(){/*这个的返回值类型得和服务提供的类型一样，下面是访问路径*/
        return restTemplate.getForEntity("http://localhost:8888/findall",List.class).getBody();//拿到访问的接口


    }
    @RequestMapping("/find2")
    public List<Customer> find2(){/*这个的返回值类型得和服务提供的类型一样，下面是访问路径*/
        return restTemplate.getForObject("http://localhost:8888/findall",List.class);//拿到访问的接口


    }
    @RequestMapping("/name/{id}")

    public  Customer findbyname(@PathVariable("id") int id){/*得到对象就不用.getbody（）*/
        System.out.println(restTemplate.getForObject("http://localhost:8888/findname/{id}",Customer.class,id));
        Customer customer=  restTemplate.getForEntity("http://localhost:8888/findname/{id}",Customer.class,id).getBody();
        System.out.println(customer);
        return  customer;
    }
     public List<List<Object>> getExcelData(SheetExportConfig sheetExportConfig){
        String interUrl = sheetExportConfig.getInterUrl();
        Map<String, Object> paramMap = new HashMap<>();
        Integer index = interUrl.indexOf("?");
        if(index!=-1){
            String s = interUrl.substring(interUrl.indexOf("?")+1);
            String[] values = s.split("&");
            for(String param:values){
                if(param.indexOf("page=")!=-1){
                    interUrl= interUrl.replace(param,"page="+sheetExportConfig.getPage());
                }
                if(param.indexOf("size=")!=-1){
                    interUrl= interUrl.replace(param,"size="+sheetExportConfig.getSize());
                }
            }
        }
        String result = this.restTemplate.getForObject(interUrl, String.class, paramMap);

        //处理json 数据 ,通用处理
        JSONObject jsonObject = JSON.parseObject(result);
        JSONObject dataObject = jsonObject.getJSONObject("data");
        if(dataObject ==null){
           log.info("解析数据，接口没有数据 interUrl={}",interUrl);
           return null;
        }
        List<List<Object>> dataList = new ArrayList<>();
        Integer totalPage=1;
        if(dataObject!=null){
             totalPage =  dataObject.get("totalPage") == null?1:dataObject.getInteger("totalPage");
             sheetExportConfig.setTotalPage(totalPage);
        }
        if(dataObject!=null && totalPage>= sheetExportConfig.getPage() ){
            JSONArray array = dataObject.getJSONArray("records");
            if(array == null){
                array = dataObject.getJSONArray("rows");
            }
            //二级
            for(int i=0;i<array.size();i++ ){
                JSONObject object = array.getJSONObject(i);
                //遍历
                List<Object> datas = new ArrayList<>();
                List<String> param = sheetExportConfig.getDataFormatList().get(0);
                for(int k=0;k<param.size();k++){
                    Object value = object.get(param.get(k));
                    datas.add(value ==null?"":value.toString());
                }
                dataList.add(datas);
            }
        }
        return  dataList;
    }
}
